package Simulator;

import java.util.Random;

public abstract class AbstactAnimal implements ActorInterface {
	// The animal's age.
	private int age;
	// Whether the animal is alive or not.
	private boolean alive;
	// The animal's position
	private Location location;
	// The age to which an animal can live.
	protected int maxAge;
	// The age at which an animal can start to breed.
	protected int breedingAge;
	// The likelihood of an animal breeding.
	protected double breedingProbability;
	// The maximum number of births.
	protected int maxLitterSize;
	// The animals food level, which is increased by eating.
	protected int foodLevel;
	// A shared random number generator to control breeding.
	private static final Random rand = new Random();

	public AbstactAnimal() {
		alive = true;
		age = 0;
	}

	public boolean isAlive() {
		return alive;
	}

	public void setDead() {
		alive = false;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(int row, int col) {
		this.location = new Location(row, col);
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public void incrementAge() {
		setAge(getAge() + 1);
		if (getAge() > maxAge) {
			setDead();
		}
	}

	protected void incrementHunger() {
		foodLevel--;
		if (foodLevel <= 0) {
			setDead();
		}
	}

	protected int breed() {
		int births = 0;
		if (maxLitterSize != 0) {
			if (canBreed() && rand.nextDouble() <= breedingProbability) {
				births = rand.nextInt(maxLitterSize) + 1;
			}
		}
		return births;
	}

	private boolean canBreed() {
		return getAge() >= breedingAge;
	}
}
